<?php
/**
 * 后台用户模型
 * @author Administrator
 */
class System_user_model extends MY_Model{

    function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 分页查询
     * @param array $data 前端获取的数据
     */
    function get_table_data($data)
    {
        // 当前页码
        $offset = $data['offset'];
        // 查询条件构造
        if(!empty($data['user_name'])){
            $this->db->like('m.user_name',$data['user_name'],'both');
        }
        if(!empty($data['phone'])){
            $this->db->like('m.phone',$data['phone'],'both');
        }
        if(!empty($data['real_name'])){
            $this->db->like('m.real_name',$data['real_name'],'both');
        }
        if(isset($data['status'])&&is_numeric($data['status'])){
            $this->db->where('m.status',$data['status']);
        }
        $this->db->order_by('m.create_time','desc');
        $this->success['total'] = $this->db->from("system_user AS m")
        ->join("system_group AS s","m.group_id=s.id","left")//连接用户组表查询
        ->count_all_results('', false); //查询总数
        $this->success['data'] = $this->db->select("m.id,m.user_name,m.phone,m.real_name,m.status,m.create_time, m.group_id, s.group_name as group_name, m.position")
        ->limit($this->limit, $offset * $this->limit)
        ->get()->result_array();
        return $this->success;
    }
    
  
    
    /**
     * 添加和编辑方法
     * @param array $data 添加和编辑数据 数组
     * @return array
     */
    function replace_system_user($data)
    {
        if (!empty($data)) {
            if (isset($data['id']) && !empty($data['id'])) {//修改方法
                $this->db->where('id', $data['id']);
                $this->db->update("system_user", $data);
                return $this->success;
            } else {//添加方法
                $data['create_time'] = time();//设置创建时间
                $data['pwd']=md5("123456");//密码MD5加密
                $this->db->insert('system_user', $data);
                return $this->success;
            }
        } else {
            return $this->param_error;//参数错误
        }
    }
    
   
    /**
     * 初始化密码
     * @param unknown $id 主键id
     * @param unknown $newpwd 密码
     */
    function update_pwd($id,$newpwd)
    {
        if(isset($id)&&is_numeric($id)){
            if ($this->db->update('system_user', array('pwd' => $newpwd), array('id' => $id))) {
                return $this->success;
            }
            return $this->failed;
        }else{
            return $this->param_error;
        }
    }
    /**
     * 修改状态
     * @param unknown $id
     * @param unknown $status
     */
    function update_system_user_status($id,$status)
    {
        if(isset($id)&&is_numeric($id)){
            //构造数据
            switch ($status) {
                case 0:
                    $data = ['status' => '1'];
                    break;
                case 1:
                    $data = ['status' => '0'];
                    break;
                default:
                    return $this->param_error;
            }
            //更新数据
            if ($this->db->where('id', $id)->update('system_user', $data)) {
                return $this->success;
            }
            return $this->failed;
        }else{
            return $this->param_error;
        }
    }
    /**
     * 验证注册名
     * @param unknown $user_name
     */
    function verify_user_name($user_name)
    {
        if(!empty($user_name)){
            $this->db->where('user_name',$user_name);
            $temp=$this->db->get('system_user')->result_array();
            $this->success['data']=0;
            if(empty($temp)){
                $this->success['data']=1;
            }
            return $this->success;
        }else {
            return $this->param_error;
        }
    }
}